package net.tv90.auth.service.impl;

import net.tv90.auth.model.SMSCode;
import net.tv90.auth.service.SMSCodeService;
import net.tv90.auth.service.SendMailService;
import net.tv90.auth.utils.SMSCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SMSCodeServiceImpl implements SMSCodeService {

    @Autowired
    private SMSCodeUtils SMSCodeUtils;

    @Autowired
    private SendMailService mailService;

    @Override
    public String sendCodeToTel(String tel) {
        return SMSCodeUtils.generateCodeForTel(tel);
    }

    @Override
    public String sendCodeToEmail(String email) {
        String code = SMSCodeUtils.generateCodeForEmail(email);
        mailService.sendCodeMailTo(email, code);
        return code;
    }

    @Override
    public boolean checkCodeForTel(SMSCode smsCode) {
        String tel = smsCode.getSms();
        String code = smsCode.getCode();
        String cacheCode = SMSCodeUtils.getCodeFromCacheForTel(tel);
        return code.equals(cacheCode);
    }

    @Override
    public boolean checkCodeForEmail(SMSCode smsCode) {
        String email = smsCode.getSms();
        String code = smsCode.getCode();
        String cacheCode = SMSCodeUtils.getCodeFromCacheForEmail(email);
        return code.equals(cacheCode);
    }
}
